package com.myzl.coal.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myzl.coal.domain.DeviceProtocol;
import com.myzl.coal.mapper.DeviceProtocolMapper;
import com.myzl.coal.service.IDeviceProtocolService;
import com.myzl.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 设备协议Service业务层处理
 *
 * @author myzl
 * @date 2022-12-01
 */
@Service
public class DeviceProtocolServiceImpl extends ServiceImpl<DeviceProtocolMapper, DeviceProtocol> implements IDeviceProtocolService
{
    @Autowired
    private DeviceProtocolMapper deviceProtocolMapper;

    /**
     * 查询设备协议
     *
     * @param id 设备协议主键
     * @return 设备协议
     */
    @Override
    public DeviceProtocol selectDeviceProtocolById(Long id)
    {
        return deviceProtocolMapper.selectDeviceProtocolById(id);
    }

    /**
     * 查询设备协议列表
     *
     * @param deviceProtocol 设备协议
     * @return 设备协议
     */
    @Override
    public List<DeviceProtocol> selectDeviceProtocolList(DeviceProtocol deviceProtocol)
    {
        return deviceProtocolMapper.selectDeviceProtocolList(deviceProtocol);
    }

    @Override
    @Cacheable(value="deviceProtocol",key="#deviceType")
    public DeviceProtocol cacheByDeviceType(Integer deviceType) {
        DeviceProtocol deviceProtocol = new DeviceProtocol();
        deviceProtocol.setDeviceType(deviceType);
        List<DeviceProtocol> deviceProtocolList = deviceProtocolMapper.selectDeviceProtocolList(deviceProtocol);
        if (!CollectionUtils.isEmpty(deviceProtocolList)){
            return deviceProtocolList.get(0);
        }
        return null;
    }

    /**
     * 新增设备协议
     *
     * @param deviceProtocol 设备协议
     * @return 结果
     */
    @Override
    public int insertDeviceProtocol(DeviceProtocol deviceProtocol)
    {
        deviceProtocol.setCreateTime(DateUtils.getNowDate());
        return deviceProtocolMapper.insertDeviceProtocol(deviceProtocol);
    }

    /**
     * 修改设备协议
     *
     * @param deviceProtocol 设备协议
     * @return 结果
     */
    @Override
    public int updateDeviceProtocol(DeviceProtocol deviceProtocol)
    {
        deviceProtocol.setUpdateTime(DateUtils.getNowDate());
        return deviceProtocolMapper.updateDeviceProtocol(deviceProtocol);
    }

    /**
     * 批量删除设备协议
     *
     * @param ids 需要删除的设备协议主键
     * @return 结果
     */
    @Override
    public int deleteDeviceProtocolByIds(Long[] ids)
    {
        return deviceProtocolMapper.deleteDeviceProtocolByIds(ids);
    }

    /**
     * 删除设备协议信息
     *
     * @param id 设备协议主键
     * @return 结果
     */
    @Override
    public int deleteDeviceProtocolById(Long id)
    {
        return deviceProtocolMapper.deleteDeviceProtocolById(id);
    }
}
